package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.commons.template.AliyunOssTemplate;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.VideoVO;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoService {

    @Autowired
    private AliyunOssTemplate ossTemplate;
    @Autowired
    private FastFileStorageClient fdsClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Reference
    private VideoApi videoApi;

    @Reference
    private UserInfoApi userInfoApi;


    public ResponseEntity saveVideo(MultipartFile thumb, MultipartFile videoFile) throws IOException {
        //把视频文件上传到FastDFS 得到视频的访问路径
        String originalFilename = videoFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        StorePath storePath = fdsClient.uploadFile(videoFile.getInputStream(), videoFile.getSize(), suffix, null);
        String videoUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();

        //把视频压缩略图上传到阿里云oss 得到图片的访问路径
        String imageUrl = ossTemplate.upload(thumb.getOriginalFilename(), thumb.getInputStream());


        //封装成Video 对象
        Video video = new Video();
        video.setVideoUrl(videoUrl);
        video.setPicUrl(imageUrl);
        video.setText("xxxx");
        video.setUserId(UserHolder.getUser().getId());
        videoApi.save(video);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity findVideoList(int page, int pagesize) {
        //分页查询视频列表
        PageResult pageResult = videoApi.findVideoList(page, pagesize);
        //2. 转换成VO
        List<Video> items = pageResult.getItems();
        List<VideoVO> voList = new ArrayList<>();
        for (Video video : items) {
            VideoVO vo = new VideoVO();
            //把视频信息封装到vo
            BeanUtils.copyProperties(video, vo);
            vo.setId(video.getId().toHexString());
            vo.setCover(video.getPicUrl());
            vo.setSignature(video.getText());
            //把发布者信息封装到vo
            UserInfo userInfo = userInfoApi.findById(video.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            //是否点赞了本视频
            vo.setHasLiked(0);
            //是否关注了本视频的作者
            Boolean hasFocus = redisTemplate.hasKey("video_focus_" + UserHolder.getUser().getId() + "_" + video.getUserId());
            vo.setHasFocus(hasFocus ? 1 : 0);
            voList.add(vo);

        }
        pageResult.setItems(voList);

        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity focus(Long targetUserId) {
        User user = UserHolder.getUser();
        videoApi.followUser(user.getId(), targetUserId);
        redisTemplate.opsForValue().set("video_focus_" + user.getId() + "_" + targetUserId, "1");

        return ResponseEntity.ok(null);
    }

    public ResponseEntity unFocus(Long targetUserId) {
        User user = UserHolder.getUser();
        videoApi.unFollowUser(user.getId(), targetUserId);
        redisTemplate.delete("video_focus_" + user.getId() + "_" + targetUserId);
        return ResponseEntity.ok(null);
    }

}
